package com.sc.dao.impl;
import com.sc.dao.UserDao;
import com.sc.entity.User;
import com.sc.utils.JDBCUtils;

import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public int add(User user) throws Exception {
        String sql= "insert into user(id,username,password)values(?,?,?)";
        return JDBCUtils.update(sql,new Object[]{user.getId(),user.getUsername(),
                user.getPassword()});
    }

    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from user where id = ?";
        return JDBCUtils.update(sql,new Object[]{id});
    }

    @Override
    public int update(User user) throws Exception {
        String sql = "update user set username= ?,password = ?";
        return JDBCUtils.update(sql,new Object[]{user.getUsername(),
                user.getPassword()});
    }

    @Override
    public User queryById(int id) throws Exception {
        String sql = "select from user where id = ?";
        return JDBCUtils.query(sql, User.class, new Object[id]);
    }

    @Override
    public List<User> queryAll() throws Exception {
        String sql = "select from user where id = ?";
        return JDBCUtils.querylist(sql, User.class, new Object[0]);
    }
}
